package com.lhys.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.Contact;
import springfox.documentation.service.SecurityScheme;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: xiaoqi
 * Date: 2021/5/6
 * Time: 下午 15:00
 * Description: swagger接口文档配置
 */
@EnableSwagger2
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.lhys")
public class SwaggerConfig {

    @Value("${environment.active}")
    private String active;

    private String profiles = "dev,test";

    @Value("${jwt.header}")
    private String tokenKey;

    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("小七")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.lhys.controller"))
                .build()
                .securitySchemes(Arrays.asList(securitySchemes()))
                .enable(profiles.contains(active)); // 根据项目环境 是否开启接口文档
    }

    private SecurityScheme securitySchemes() {
        return new ApiKey(tokenKey, tokenKey, "header");
    }

    private ApiInfo apiInfo() {

        Contact contact = new Contact("xiaoqi", "http://sunjun7.top", "2860253651@qq.com");

        return new ApiInfo(
                "asset的SwaggerApi文档",
                "我们是最棒的",
                "1.0",
                "http://sunjun7.top",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }
}